﻿Module PassWord
    Private Const PWD_KEY As String = "key"
   
    Public Function GBKEncode(ByVal sInput As String) As String  '加密
        Dim ret_GBKEncode As String = ""
        Dim i As Integer
        Dim startIndex As Integer = 0
        Dim endIndex As Integer
        Dim x() As Byte = System.Text.Encoding.Default.GetBytes(sInput)

        endIndex = x.Length - 1
        For i = startIndex To endIndex
            ret_GBKEncode &= Hex(x(i))
        Next
        Return ret_GBKEncode
    End Function

    Public Function GBKDecode(ByVal sInput As String) As String  '解密
        ' sInput = sInput.Replace("%", "")

        Dim ret_GBKDecode As String = ""
        Dim sLen As Integer = sInput.Length
        Dim n As Integer = sLen \ 2
        Dim sBytes(0 To n - 1) As Byte
        '转化为字节码
        For i As Integer = 1 To n
            sBytes(i - 1) = CByte("&H" & sInput.Substring(2 * i - 2, 2))
        Next
        '将字节码转化为字符串
        ret_GBKDecode = System.Text.Encoding.Default.GetString(sBytes)
        Return ret_GBKDecode
    End Function

    Public Function Encode(ByVal Encode_pwd As String) As String
        Dim Save_pwd As String = ""
        Dim Code As Double
        ' Code = Val(GBKEncode(Encode_pwd))
        Code = Val(GBKEncode(Encode_pwd)) + Val(GBKEncode(PWD_KEY))
        Save_pwd = Code.ToString
        Return Save_pwd
    End Function

    Public Function Decode(ByVal Decode_pwd As String) As String
        Dim Reall_pwd As String = ""

        Decode_pwd = (Val(Decode_pwd) - Val(GBKEncode(PWD_KEY))).ToString
        Reall_pwd = GBKDecode(Decode_pwd)

        Return Reall_pwd
    End Function


End Module
